import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
var _excluded = ["organizationId"];
import request from "../utils/request";
import { getEnvConfig } from "../utils/iocUtils";

var _getEnvConfig = getEnvConfig(),
    AUTH_SELF_URL = _getEnvConfig.AUTH_SELF_URL,
    CPAAS_IAM = _getEnvConfig.CPAAS_IAM,
    CPAAS_PLATFORM = _getEnvConfig.CPAAS_PLATFORM;

export function query() {
  return _query.apply(this, arguments);
} // export async function queryCurrent() {
//   return request('/api/currentUser');
// }

function _query() {
  _query = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            return _context.abrupt("return", request('/api/users'));

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _query.apply(this, arguments);
}

export function queryCurrent() {
  return _queryCurrent.apply(this, arguments);
}
/**
 * @async
 * @function queryTenant - 查询用户租户列表数据
 */

function _queryCurrent() {
  _queryCurrent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request(AUTH_SELF_URL));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _queryCurrent.apply(this, arguments);
}

export function queryTenant() {
  return _queryTenant.apply(this, arguments);
}
/**
 * 组织层查询当前登录用户默认角色
 * @async
 * @function queryDefaultRole
 * @param {!number} organizationId - 组织ID
 * @returns {object} fetch Promise
 */

function _queryTenant() {
  _queryTenant = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/users/self-tenants"), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _queryTenant.apply(this, arguments);
}

export function queryDefaultRole() {
  return _queryDefaultRole.apply(this, arguments);
}
/**
 * 组织层查询分配给当前登录用户的角色
 * @async
 * @function queryRoleList
 * @param {!number} organizationId - 组织ID
 * @returns {object} fetch Promise
 */

function _queryDefaultRole() {
  _queryDefaultRole = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/member-roles/current-role")));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _queryDefaultRole.apply(this, arguments);
}

export function queryRoleList() {
  return _queryRoleList.apply(this, arguments);
}
/**
 * 缓存当前用户的角色
 * @async
 * @function updateCurrentRole
 * @param {!number} roleId
 * @returns {object} fetch Promise
 */

function _queryRoleList() {
  _queryRoleList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/member-roles/self-roles")));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _queryRoleList.apply(this, arguments);
}

export function updateCurrentRole(_x) {
  return _updateCurrentRole.apply(this, arguments);
}
/**
 * 缓存当前用户的租户
 * @async
 * @function updateCurrentRole
 * @param {!number} roleId
 * @returns {object} fetch Promise
 */

function _updateCurrentRole() {
  _updateCurrentRole = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(roleId) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request("".concat(CPAAS_IAM, "/v1/users/roles"), {
              method: 'PUT',
              query: {
                roleId: roleId
              }
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _updateCurrentRole.apply(this, arguments);
}

export function updateCurrentTenant(_x2) {
  return _updateCurrentTenant.apply(this, arguments);
}
/**
 * 更新默认租户
 * @async
 * @function updateDefaultTenant
 * @param {!number} tenantId
 * @returns {object} fetch Promise
 */

function _updateCurrentTenant() {
  _updateCurrentTenant = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(tenantId) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(CPAAS_IAM, "/v1/users/tenant-id"), {
              method: 'PUT',
              query: {
                tenantId: tenantId
              }
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _updateCurrentTenant.apply(this, arguments);
}

export function updateDefaultTenant(_x3) {
  return _updateDefaultTenant.apply(this, arguments);
}
/**
 * @async
 * @function queryTenant - 查询列表数据
 */

function _updateDefaultTenant() {
  _updateDefaultTenant = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(tenantId) {
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            return _context8.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/users/default-tenant"), {
              method: 'PUT',
              query: {
                tenantId: tenantId
              }
            }));

          case 1:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _updateDefaultTenant.apply(this, arguments);
}

export function fetchDataHierarchiesList(_x4) {
  return _fetchDataHierarchiesList.apply(this, arguments);
}
/**
 * @async
 * @function queryTenant - 查询列表数据
 */

function _fetchDataHierarchiesList() {
  _fetchDataHierarchiesList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(params) {
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            return _context9.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(params.organizationId, "/data-hierarchies/switch/value"), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _fetchDataHierarchiesList.apply(this, arguments);
}

export function fetchDataHierarchiesSelectList(_x5) {
  return _fetchDataHierarchiesSelectList.apply(this, arguments);
}
/**
 * @async
 * @function queryTenant - 查询用户租户列表数据
 */

function _fetchDataHierarchiesSelectList() {
  _fetchDataHierarchiesSelectList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(params) {
    var organizationId, others;
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            organizationId = params.organizationId, others = _objectWithoutProperties(params, _excluded);
            return _context10.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/data-hierarchies/switch/value/details"), {
              method: 'GET',
              query: others
            }));

          case 2:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _fetchDataHierarchiesSelectList.apply(this, arguments);
}

export function switchDataHierarchies() {
  return _switchDataHierarchies.apply(this, arguments);
}
/**
 * @async
 * @function queryTenant - 查询用户租户列表数据
 */

function _switchDataHierarchies() {
  _switchDataHierarchies = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11() {
    var params,
        dataHierarchyCode,
        _params$dataHierarchy,
        dataHierarchyValue,
        _params$dataHierarchy2,
        dataHierarchyMeaning,
        organizationId,
        _args11 = arguments;

    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            params = _args11.length > 0 && _args11[0] !== undefined ? _args11[0] : {};
            dataHierarchyCode = params.dataHierarchyCode, _params$dataHierarchy = params.dataHierarchyValue, dataHierarchyValue = _params$dataHierarchy === void 0 ? '' : _params$dataHierarchy, _params$dataHierarchy2 = params.dataHierarchyMeaning, dataHierarchyMeaning = _params$dataHierarchy2 === void 0 ? '' : _params$dataHierarchy2, organizationId = params.organizationId;
            return _context11.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/data-hierarchies/switch"), {
              method: 'PUT',
              query: {
                dataHierarchyCode: dataHierarchyCode,
                dataHierarchyValue: dataHierarchyValue,
                dataHierarchyMeaning: dataHierarchyMeaning
              }
            }));

          case 3:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _switchDataHierarchies.apply(this, arguments);
}

export function queryDataHierarchies() {
  return _queryDataHierarchies.apply(this, arguments);
}

function _queryDataHierarchies() {
  _queryDataHierarchies = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12() {
    var params,
        organizationId,
        _args12 = arguments;
    return _regeneratorRuntime.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            params = _args12.length > 0 && _args12[0] !== undefined ? _args12[0] : {};
            organizationId = params.organizationId;
            return _context12.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/data-hierarchies/switch/display-style/value"), {
              method: 'GET'
            }));

          case 3:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  }));
  return _queryDataHierarchies.apply(this, arguments);
}